﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entity;
using DataControls;

namespace GiaoVien_QuanLy_DeTai
{
    public partial class LOPHOC_Form : Form
    {

        LOPHOC lophoc = new LOPHOC();
        LOPHOC_Data lophoc_Data = new LOPHOC_Data();

        HEDAOTAO_Data hedaotao_Data = new HEDAOTAO_Data();
        GIAOVIEN_Data giaovien_Data = new GIAOVIEN_Data();
        KHOA_Data khoa_Data = new KHOA_Data();

        THAMSO thamso = new THAMSO();





        public LOPHOC_Form()
        {
            InitializeComponent();
        }

        private void LOPHOC_Form_Load(object sender, EventArgs e)
        {
            txtMaLop.Text = "LH";
            txtHeSoGD.Text = "";
            txtSiSo.Text = "0";
            txtTenLop.Text = "";
            cbGiaoVienChuNhiem.Text = "";
            cbHeDaoTao.Text = "";
            cbKhoa.Text = "";

            cbGiaoVienChuNhiem.Items.Clear();
            cbHeDaoTao.Items.Clear();
            cbKhoa.Items.Clear();

            khoa_Data.khoa_Combobox(cbKhoa);
            giaovien_Data.giaovien_Combobox(cbGiaoVienChuNhiem);
            hedaotao_Data.hedaotao_Combobox(cbHeDaoTao);

        }

        private void btnHienThi_Click(object sender, EventArgs e)
        {
            lophoc.hienthi_LopHoc(dtgLopHoc);
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtMaLop.Text == "")
            {
                MessageBox.Show("Ban chua chon Ma Lop");
                txtMaLop.Focus();
                return;
            }
            else if (txtTenLop.Text == "")
            {
                MessageBox.Show("Ban chua nhap Ten Lop");
                txtTenLop.Focus();
                return;
            }               
            else if(txtSiSo.Text=="")
            {
                MessageBox.Show("Ban chua nhap si so");
                txtSiSo.Focus();
                return;
            }
            else if(txtHeSoGD.Text=="")
            {
                MessageBox.Show("Ban chua nhap he so Giang Day");
                txtHeSoGD.Focus();
                return;
            }
            else if (cbGiaoVienChuNhiem.Text == "")
            {
                MessageBox.Show("Ban chua chon Giao Vien");
                return;
            }
            else if (cbHeDaoTao.Text == "")
            {
                MessageBox.Show("Ban chua chon He Dao Tao");
                return;
            }
            else if (cbKhoa.Text == "")
            {
                MessageBox.Show("Ban chua chon Khoa");
                return;
            }
            else if (lophoc_Data.kiemtra_MaLopHoc(txtMaLop.Text.ToString()))
            {
                MessageBox.Show("Ban nhap trung ma Lop Hoc");
                txtMaLop.Focus();
                return;
            }
            else if (lophoc_Data.kiemtra_TenLop(txtTenLop.Text.ToString()))
            {
                MessageBox.Show("Ban nhap trung Ten Lop Hoc");
                txtTenLop.Focus();
                return;
            }
            else
            {
                lophoc.tenLop = txtTenLop.Text.ToString();
                lophoc.maLop = txtMaLop.Text.ToString();
                lophoc.maKhoa = ((ListItem)(cbKhoa.SelectedItem)).value;
                lophoc.maGV = ((ListItem)(cbGiaoVienChuNhiem.SelectedItem)).value;
                lophoc.maHDT = ((ListItem)(cbHeDaoTao.SelectedItem)).value;

                try
                {
                    lophoc.siso = Convert.ToInt32(txtSiSo.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban chua nhap dung si so \n" + ex.Message);
                    txtSiSo.Focus();
                    return;
                }
                try
                {
                    lophoc.hesoGD = (float)(Convert.ToDouble(txtSiSo.Text));
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban chua nhap dung He So Giang Day \n" + ex.Message);
                    txtHeSoGD.Focus();
                    return;
                }

                lophoc.them_LopHoc(lophoc);
                lophoc.hienthi_LopHoc(dtgLopHoc);

                txtMaLop.Text = "LH";
                txtHeSoGD.Text = "";
                txtSiSo.Text = "0";
                txtTenLop.Text = "";
                cbGiaoVienChuNhiem.Text = "";
                cbHeDaoTao.Text = "";
                cbKhoa.Text = "";

                cbGiaoVienChuNhiem.Items.Clear();
                cbHeDaoTao.Items.Clear();
                cbKhoa.Items.Clear();

                khoa_Data.khoa_Combobox(cbKhoa);
                giaovien_Data.giaovien_Combobox(cbGiaoVienChuNhiem);
                hedaotao_Data.hedaotao_Combobox(cbHeDaoTao);
            }
                  
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (txtMaLop.Text == "")
            {
                MessageBox.Show("Ban chua chon Ma Lop");
                txtMaLop.Focus();
                return;
            }
            else if (txtTenLop.Text == "")
            {
                MessageBox.Show("Ban chua nhap Ten Lop");
                txtTenLop.Focus();
                return;
            }
            else if (txtSiSo.Text == "")
            {
                MessageBox.Show("Ban chua nhap si so");
                txtSiSo.Focus();
                return;
            }
            else if (txtHeSoGD.Text == "")
            {
                MessageBox.Show("Ban chua nhap he so Giang Day");
                txtHeSoGD.Focus();
                return;
            }
            else if (cbGiaoVienChuNhiem.Text == "")
            {
                MessageBox.Show("Ban chua chon Giao Vien");
                return;
            }
            else if (cbHeDaoTao.Text == "")
            {
                MessageBox.Show("Ban chua chon He Dao Tao");
                return;
            }
            else if (cbKhoa.Text == "")
            {
                MessageBox.Show("Ban chua chon Khoa");
                return;
            }
            else
            {
                lophoc.tenLop = txtTenLop.Text.ToString();
                lophoc.maLop = txtMaLop.Text.ToString();
                lophoc.maKhoa = ((ListItem)(cbKhoa.SelectedItem)).value;
                lophoc.maGV = ((ListItem)(cbGiaoVienChuNhiem.SelectedItem)).value;
                lophoc.maHDT = ((ListItem)(cbHeDaoTao.SelectedItem)).value;

                try
                {
                    if (Convert.ToInt32(txtSiSo.Text) < 0)
                    {
                        MessageBox.Show("Ban chua nhap dung si so");
                        txtSiSo.Focus();
                        return;
                    }
                    lophoc.siso = Convert.ToInt32(txtSiSo.Text);
                
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban chua nhap dung si so \n" + ex.Message);
                    txtSiSo.Focus();
                    return;
                }
                try
                {
                    if (Convert.ToDouble(txtHeSoGD.Text) < 0)
                    {
                        MessageBox.Show("Ban chua nhap dung he so Giang Day");
                        txtSiSo.Focus();
                        return;
                    }
                    lophoc.hesoGD = (float)(Convert.ToDouble(txtHeSoGD.Text));
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Ban chua nhap dung He So Giang Day \n" + ex.Message);
                    txtHeSoGD.Focus();
                    return;
                }

                lophoc.sua_LopHoc(lophoc);
                lophoc.hienthi_LopHoc(dtgLopHoc);

                txtMaLop.Text = "LH";
                txtHeSoGD.Text = "";
                txtSiSo.Text = "0";
                txtTenLop.Text = "";
                cbGiaoVienChuNhiem.Text = "";
                cbHeDaoTao.Text = "";
                cbKhoa.Text = "";

                cbGiaoVienChuNhiem.Items.Clear();
                cbHeDaoTao.Items.Clear();
                cbKhoa.Items.Clear();

                khoa_Data.khoa_Combobox(cbKhoa);
                giaovien_Data.giaovien_Combobox(cbGiaoVienChuNhiem);
                hedaotao_Data.hedaotao_Combobox(cbHeDaoTao);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtMaLop.Text == "")
            {
                MessageBox.Show("Ban chua chon Ma Lop");
                txtMaLop.Focus();
                return;
            }
            else if (lophoc_Data.kiemtraMaLH_GiangDay(txtMaLop.Text.ToString()))
            {
                MessageBox.Show("Ban chua xoa Lop Hoc Trong Giang Day");
                return;
            }
            else
                lophoc.xoa_LopHoc(txtMaLop.Text.ToString());
            lophoc.hienthi_LopHoc(dtgLopHoc);

            txtMaLop.Text = "LH";
            txtHeSoGD.Text = "";
            txtSiSo.Text = "0";
            txtTenLop.Text = "";
            cbGiaoVienChuNhiem.Text = "";
            cbHeDaoTao.Text = "";
            cbKhoa.Text = "";
        }

        private void dtgLopHoc_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dtgLopHoc.AllowUserToAddRows = false;
            int dong = e.RowIndex;
            txtMaLop.Text = dtgLopHoc.Rows[dong].Cells[0].Value.ToString();
            txtTenLop.Text = dtgLopHoc.Rows[dong].Cells[1].Value.ToString();
            txtSiSo.Text = dtgLopHoc.Rows[dong].Cells[4].Value.ToString();
            txtHeSoGD.Text = dtgLopHoc.Rows[dong].Cells[6].Value.ToString();

            String makhoa = dtgLopHoc.Rows[dong].Cells[2].Value.ToString();
            lophoc_Data.layten_Khoa(makhoa, cbKhoa);
            String mahedaotao = dtgLopHoc.Rows[dong].Cells[3].Value.ToString();
            lophoc_Data.layten_Khoa(mahedaotao, cbHeDaoTao);
            String magiaovien = dtgLopHoc.Rows[dong].Cells[6].Value.ToString();
            lophoc_Data.layten_Khoa(magiaovien, cbGiaoVienChuNhiem);
        }

       

        private void txtSiSo_MouseLeave(object sender, EventArgs e)
        {
            int siso = 0 ;
            try
            {
                if (Convert.ToInt32(txtSiSo.Text) < 0)
                {
                    MessageBox.Show("Ban chua nhap dung si so");
                    txtSiSo.Focus();
                    return;
                }
                siso = Convert.ToInt32(txtSiSo.Text);

            }
            catch (Exception ex)
            {
                MessageBox.Show("Kieu du lieu si so chua dung \n"+ ex.Message);
                txtSiSo.Focus();
            }
             
            txtHeSoGD.Text = thamso.heso_siso(siso).ToString();
        }

        private void cbKhoa_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbKhoa.Text == "")
            {
                MessageBox.Show("Ban chua chon Khoa");
                return;
            }
            cbGiaoVienChuNhiem.Items.Clear();
            cbGiaoVienChuNhiem.Text = "";
            String makhoa = ((ListItem)(cbKhoa.SelectedItem)).value;
            giaovien_Data.giaovientheokhoa_Combobox(cbGiaoVienChuNhiem, makhoa);
        }

        private void cbGiaoVienChuNhiem_SelectedIndexChanged(object sender, EventArgs e)
        {
            String magiaovien = ((ListItem)(cbGiaoVienChuNhiem.SelectedItem)).value;
            giaovien_Data.thongtin_GiaoVien(dtgGiaoVien,magiaovien);
        }

        private void txtMaLop_MouseClick(object sender, MouseEventArgs e)
        {
            txtMaLop.Text = "LH";
            txtHeSoGD.Text = "";
            txtSiSo.Text = "0";
            txtTenLop.Text = "";
        }

        private void rdTenLop_CheckedChanged(object sender, EventArgs e)
        {
            if (rdTenGiaoVien.Checked)
            {
                txtTGV.Visible = true;
                txtTGV.Text = "";
            }
            else if (rdTenLop.Checked)
            {
                txtTL.Visible = true;
                txtTL.Text = "";
            }
        }

        private void rdTenGiaoVien_CheckedChanged(object sender, EventArgs e)
        {
            if (rdTenGiaoVien.Checked)
            {
                txtTGV.Visible = true;
                txtTL.Visible = false;
                txtTGV.Text = "";
            }
            else if (rdTenLop.Checked)
            {
                txtTGV.Visible = false;
                txtTL.Visible = true;
                txtTL.Text = "";
            }
        }


        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            if (rdTenLop.Checked)
            {
                lophoc_Data.timkiemtheo_Lop(dtgLopHoc, txtTL.Text.ToString());
                txtTL.Text = "";
            }
            else
            {
                lophoc_Data.timkiemtheo_GiaoVien(dtgLopHoc,txtTGV.Text.ToString());
                txtTGV.Text = "";
            }
        }

        

        

    }
}
